#include "interationredis.hpp"
#include <iostream>
#include "myLog4.h"

const std::string REDISIP = ConfigManager::GetRedisIP();
const int REDISPORT = ConfigManager::GetRedisPort();
const std::string REDISPASSWORD = ConfigManager::GetRedisPassword();

void initializeRedis(const std::string& IP, const int& PORT, const std::string& PASSWORD){
    /**
    * @brief Redis初始化，若无则无法在线程中直接插入成功
    * @param Redis的IP、端口与密码
    * @return None
    */
   try {
        RedisConnect::Setup(IP, PORT, PASSWORD);
        std::shared_ptr<RedisConnect> redis = RedisConnect::Instance();
        redis->select(11);
        LOG_INFO("[log4]" << "Redis数据库初始化成功!");
    } catch (const std::exception& e) {
        LOG_ERROR("[log4]" << LOG4CPLUS_TEXT("Redis数据库初始化失败 : ") << e.what());
    }
}

void subscriptionInfo2Redis(const std::string& uid, const std::string& postData, 
    const std::string& IP, const int& PORT, const std::string& PASSWORD){
    /**
    * @brief 将postData传入数据库对用的uid位置，会根据":"自动划分子表
    * @param uid:用于分区与记录不同无人机，要求可以转换为整数
    * @return None
    */
    int UAVIP = std::stoi(uid);
    RedisConnect::Setup(IP, PORT, PASSWORD);
    shared_ptr<RedisConnect> redis = RedisConnect::Instance();
    redis->select(11);
    // 在这里执行你的任务
    redis->set("uav:" + to_string(UAVIP) +":realtime", postData);
}

std::string getSingnalSubscriptionInfoFromRedis(const std::string& uid, 
    const std::string& IP, const int& PORT, const std::string& PASSWORD){

    int UAVIP = std::stoi(uid);
    RedisConnect::Setup(IP, PORT, PASSWORD);
    std::shared_ptr<RedisConnect> redis = RedisConnect::Instance();
    redis->select(11);
    std::string data = redis->get("uav:" + std::to_string(UAVIP) + ":realtime");
    return data;
}

json getGpsFromRedis(const std::string& uid,
    const std::string& IP, const int& PORT, const std::string& PASSWORD){
    /**
    * @brief 查看最新的单个无人机的GPS数据
    * @param uid 无人机编号
    * @return {"x":1171266124,"y":318300376,"z":49249}
    */
    std::string data = getSingnalSubscriptionInfoFromRedis(uid, IP, PORT, PASSWORD);
    json jsonData = JsonOperater::string2Json(data);
    // 检查并提取 TOPIC_GPS_POSITION 数据
    if (jsonData.contains("TOPIC_GPS_POSITION") && jsonData["TOPIC_GPS_POSITION"].is_object()) {
        return jsonData["TOPIC_GPS_POSITION"];
    } else {
        LOG_INFO("[log4]" << LOG4CPLUS_TEXT("该无人机GPS数据不存在:") << uid);
        return json();
    }
}

json getMultiGpsFromRedis(vector<string> uids,
    const std::string& IP, const int& PORT, const std::string& PASSWORD){
    /**
    * @brief 查看最新的多个无人机的GPS数据
    * @param uids 无人机编号
    * @return {
                "201": {
                    "x": 1171266124,
                    "y": 318300376,
                    "z": 49249
                },
                "202": {
                    "x": 1171266125,
                    "y": 318300377,
                    "z": 49250
                },
                "203": {
                    "x": 1171266126,
                    "y": 318300378,
                    "z": 49251
                }
            }
    */
    json result;
    for (const std::string& uid : uids) {
        json gpsData = getGpsFromRedis(uid, IP, PORT, PASSWORD);
        if (!gpsData.is_null()) {
            result[uid] = gpsData;
        }
    }
    LOG_INFO("[log4]" << LOG4CPLUS_TEXT("成功查找到如下GPS:") << result);
    return result;
}